package com.zmj.sy.mom.srv.aps.controller;

import com.zmj.sy.mom.srv.aps.bean.dto.staff.*;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.vo.base.*;
import com.zmj.sy.mom.srv.aps.bean.vo.group.GroupAddReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.grouprefactor.*;
import com.zmj.sy.mom.srv.aps.feign.StaffApiClient;
import com.zmj.sy.mom.srv.aps.mapper.GroupMapper;
import com.zmj.sy.mom.srv.aps.service.GroupRefactorService;
import com.zmj.sy.mom.srv.aps.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Api(tags = "工作组-重构")
@RestController
@RequestMapping("/groupRefact")
public class GroupRefactController {

    private final GroupRefactorService groupRefactorService;

    private final StaffApiClient staffApiClient;

    private final GroupMapper groupMapper;

    private final GroupService groupService;

    @ApiOperation("1.4.1 新增组")
    @PostMapping("/groupAddDefault")
    public GroupAddResVo groupAddDefault(@RequestBody BaseIdReqVo reqVo1) {
        GroupAddReqVo reqVo = new GroupAddReqVo();

        reqVo.setMyCode("0-0_DEFAULT#_01");
        reqVo.setName("0-0默认01");
        reqVo.setRemark("JPJ");
        reqVo.setSpanId(6);
        reqVo.setWorkshopTaskTitleVal("5");
        reqVo.setStartReportWorkVal("parentToparent");
        reqVo.setTeamId(null);
        reqVo.setTeamName("");
        reqVo.setWorkAbility(new BigDecimal(1));
        reqVo.setShiftTypeId(null);
        reqVo.setUserCount(new BigDecimal(1));
        reqVo.setLineId(reqVo1.getId());

        Integer id = groupService.add(reqVo);
        GroupAddResVo resVo = new GroupAddResVo();
        resVo.setSourceId(id);
        resVo.setMyCode("0-0_DEFAULT#_01");
        resVo.setName("0-0默认01");
        resVo.setType(3);
        resVo.setId(resVo.getType() + "$$$" + id);
        resVo.setPid((resVo.getType()-1) + "$$$" + reqVo1.getId());
        return resVo;
    }

    @ApiOperation("工厂查看")
    @GetMapping("/factoryGet")
    public GroupRefactFactoryGetResVo factoryGet(BaseIdReqVo reqVo) {
        return groupRefactorService.factoryGet(reqVo);
    }

    @ApiOperation("工厂修改")
    @PostMapping("/factoryEdit")
    public BaseResVo factoryEdit(GroupRefactFactoryEditReqVo reqVo) {
        groupRefactorService.factoryEdit(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工厂列表")
    @GetMapping("/factoryList")
    public BaseListResVo<GroupRefactFactoryListResVo> factoryList() {
        return groupRefactorService.factoryList();
    }

    @ApiOperation("产线查看")
    @GetMapping("/lineGet")
    public GroupRefactLineGetResVo lineGet(BaseIdReqVo reqVo) {
        return groupRefactorService.lineGet(reqVo);
    }

    @ApiOperation("产线修改")
    @PostMapping("/lineEdit")
    public BaseResVo lineEdit(GroupRefactLineEditReqVo reqVo) {
        groupRefactorService.lineEdit(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("产线新增")
    @PostMapping("/lineAdd")
    public GroupRefactLineAddResVo lineAdd(GroupRefactLineAddReqVo reqVo) {
        Integer id = groupRefactorService.lineAdd(reqVo);
        GroupRefactLineAddResVo groupRefactLineAddResVo = new GroupRefactLineAddResVo();
        groupRefactLineAddResVo.setSourceId(id);
        return groupRefactLineAddResVo;
    }

    @ApiOperation("产线列表")
    @GetMapping("/lineList")
    public BaseListResVo<GroupRefactLineListResVo> lineList(BaseIdReqVo reqVo) {
        return groupRefactorService.lineList(reqVo);
    }

    @ApiOperation("产线删除")
    @PostMapping("/lineDelete")
    public BaseResVo lineDelete(BaseIdReqVo reqVo) {
        groupRefactorService.lineDelete(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("产线下拉框")
    @GetMapping("/lineSelect")
    public BaseSelectResVo lineSelect() {
        return groupRefactorService.lineSelect();
    }

    @ApiOperation("工作组-列表")
    @PostMapping("/groupTree")
    public BaseListResVo<GroupRefactGroupTreeResVo> groupTree() {
        List<GroupRefactGroupTreeResVo> resVo = groupRefactorService.groupTree();
        resVo.sort(Comparator.comparing(GroupRefactGroupTreeResVo::getName));
        return new BaseListResVo<>(resVo);
    }

    @ApiOperation("工作组-单个详情")
    @GetMapping("/groupGet")
    public GroupRefactGroupGetResVo groupGet(BaseIdReqVo reqVo) {
        return groupRefactorService.groupGet(reqVo);
    }

    @ApiOperation("工作组-班组人员")
    @GetMapping("/groupUser")
    public GroupRefactGroupUserResVo groupUser(GroupRefactGroupUserReqVo reqVo) {
        StaffUserListReqDto r = new StaffUserListReqDto();

        Group group = groupMapper.selectById(reqVo.getId());

        r.setSize(9999);
        r.setPage(1);
        Result<StaffPage<StaffUserListResDto>> pageResult = staffApiClient.userList(r);

        List<GroupRefactGroupUserUsedResVo> userList = pageResult.getData()
                .getItems()
                .stream()
                .map(e -> {
                    GroupRefactGroupUserUsedResVo mapBean = new GroupRefactGroupUserUsedResVo();
                    BeanUtils.copyProperties(e, mapBean);
                    return mapBean;
                })
                .collect(Collectors.toList());

        GroupRefactGroupUserResVo resVo = new GroupRefactGroupUserResVo();
        resVo.setTeamId(group.getTeamId());
        resVo.setTeamName(group.getTeamName());
        resVo.setUsed(userList.stream().filter(e -> Objects.equals(group.getTeamId(), e.getTeamId())).collect(Collectors.toList()));
        resVo.setAll(userList.stream().filter(e -> !Objects.equals(group.getTeamId(), e.getTeamId())).collect(Collectors.toList()));
        return resVo;
    }

    @ApiOperation("工作组-班组列表")
    @GetMapping("/teamList")
    public BaseListResVo<GroupRefactTeamListResVo> teamList() {
        StaffTeamListReqDto r = new StaffTeamListReqDto();
        r.setSize(9999);
        r.setPage(1);
        Result<StaffPage<StaffTeamListResDto>> pageResult = staffApiClient.teamList(r);

        List<GroupRefactTeamListResVo> teamList = pageResult.getData()
                .getItems()
                .stream()
                .map(e -> {
                    GroupRefactTeamListResVo mapBean = new GroupRefactTeamListResVo();
                    BeanUtils.copyProperties(e, mapBean);
                    return mapBean;
                })
                .collect(Collectors.toList());

        return new BaseListResVo<>(teamList);
    }

    @ApiOperation("获取工位信息")
    @GetMapping("/stationGet")
    public GroupRefactStationGetResVo stationGet(BaseIdReqVo reqVo) {
        return groupRefactorService.stationGet(reqVo);
    }

    @ApiOperation("工位新增")
    @PostMapping("/stationAdd")
    public BaseResVo stationAdd(@RequestBody GroupRefactStationAddReqVo reqVo) {
        groupRefactorService.stationAdd(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工位新增默认")
    @PostMapping("/stationAddDefault")
    public BaseResVo stationAddDefault(@RequestBody GroupRefactStationAddReqVo reqVo1) {
        GroupRefactStationAddReqVo reqVo = new GroupRefactStationAddReqVo();

        reqVo.setGroupId(reqVo1.getGroupId());
        reqVo.setMyCode("2-1_SC_DEFAULT_01");
        reqVo.setName("2-1默认工位01#工位");
        reqVo.setDeviceType(null);
        reqVo.setDeviceCode(null);
        reqVo.setDeviceModel(null);
        reqVo.setState(1);
        reqVo.setWorkAbility(new BigDecimal(1));
        reqVo.setCompleteMode(null);
        reqVo.setEnterTime(null);
        reqVo.setOutTime(null);
        reqVo.setPrepTime(null);
        reqVo.setChangTime(null);
        reqVo.setWorkDir(null);
        reqVo.setProductivity(new BigDecimal("1.2"));
        reqVo.setSmallTransfer(0);
        reqVo.setAttr(null);
        reqVo.setEmployeeId(null);
        reqVo.setWorkTemplateId(null);
        reqVo.setStationStatus(4);
        reqVo.setStaffCode(null);
        reqVo.setStaffName(null);

        groupRefactorService.stationAdd(reqVo);
        return BaseResVo.ok();
    }


    @ApiOperation("工位修改")
    @PostMapping("/stationEdit")
    public BaseResVo stationEdit(@RequestBody GroupRefactStationEditReqVo reqVo) {
        groupRefactorService.stationEdit(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工位删除")
    @PostMapping("/stationDel")
    public BaseResVo stationDel(BaseIdReqVo reqVo) {
        groupRefactorService.stationDel(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工序列表")
    @PostMapping("/phaseList")
    public GroupRefactPhaseListResVo phaseList(GroupRefactPhaseListReqVo reqVo) {
        return groupRefactorService.phaseList(reqVo);
    }

    @ApiOperation("工序详情")
    @PostMapping("/phaseDetail")
    public GroupRefactPhaseDetailResVo phaseDetail(BaseIdReqVo reqVo) {
        return groupRefactorService.phaseDetail(reqVo);
    }

    @ApiOperation("工序新增")
    @PostMapping("/phaseAdd")
    public BaseResVo phaseAdd(GroupRefactPhaseAddReqVo reqVo) {
        groupRefactorService.phaseAdd(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工序修改")
    @PostMapping("/phaseEdit")
    public BaseResVo phaseEdit(GroupRefactPhaseEditReqVo reqVo) {
        groupRefactorService.phaseEdit(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工序删除")
    @PostMapping("/phaseDel")
    public BaseResVo phaseDel(BaseIdReqVo reqVo) {
        groupRefactorService.phaseDel(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("班制选择框")
    @GetMapping("/shiftTypeSelect")
    public BaseListResVo<GroupRefactShiftTypeSelectResVo> shiftTypeSelect() {
        return new BaseListResVo<>(groupRefactorService.shiftTypeSelect());
    }


}
